package com.pms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pms.constant.MessageConstant;
import com.pms.dto.BuildingPageQueryDTO;
import com.pms.dto.BuildingSaveDTO;
import com.pms.dto.BuildingUpdateDTO;
import com.pms.entity.Building;
import com.pms.entity.Community;
import com.pms.entity.Owner;
import com.pms.exception.DeletionNotAllowedException;
import com.pms.exception.InsertNotAllowException;
import com.pms.mapper.BuildingMapper;
import com.pms.mapper.CommunityMapper;
import com.pms.mapper.OwnerMapper;
import com.pms.result.PageResult;
import com.pms.service.BuildingService;
import com.pms.service.CommunityService;
import com.pms.vo.BuildingPageQueryVO;
import com.pms.vo.BuildingSelectVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private OwnerMapper ownerMapper;
    @Autowired
    private CommunityMapper communityMapper;

    /**
     * 新增楼栋
     * @param buildingSaveDTO
     */
    @Transactional
    public void save(BuildingSaveDTO buildingSaveDTO) {
        Building buildingMapperByIdAndNumber = buildingMapper.getByIdAndNumber(buildingSaveDTO.getCommunityId(), buildingSaveDTO.getNumber());
        //判断是否存在该楼栋记录
        if (buildingMapperByIdAndNumber != null) {
            throw new InsertNotAllowException(MessageConstant.RECORD_EXIST);
        }

        Building building = new Building();
        BeanUtils.copyProperties(buildingSaveDTO, building);
        building.setUpdateDate(LocalDate.now());

        Owner ownerList = ownerMapper.getByOwnerName(buildingSaveDTO.getOwnerName());
        Integer ownerId;
        if (ownerList==null) {
            //如果楼栋关联的业主在数据库没有记录的话，新增一条含有名字,小区id的业主记录
            Owner owner = new Owner();
            owner.setName(buildingSaveDTO.getOwnerName());
            owner.setCommunityId(buildingSaveDTO.getCommunityId());
            ownerMapper.insert(owner);
            ownerId=owner.getId();
        }else {
            // 处理存在业主记录的情况
            ownerId = ownerList.getId();
        }

        building.setOwnerId(ownerId);
        buildingMapper.insert(building);
    }

    /**
     * 楼栋分页查询
     * @param buildingPageQueryDTO
     * @return
     */
    public PageResult pageQuery(BuildingPageQueryDTO buildingPageQueryDTO) {
        PageHelper.startPage(buildingPageQueryDTO.getPage(), buildingPageQueryDTO.getPageSize());
        Page<BuildingPageQueryVO> page=buildingMapper.pageQuery(buildingPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id批量删除
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            buildingMapper.delete(id);
        }
    }

    /**
     * 修改楼栋信息
     * @param buildingUpdateDTO
     */
    @Transactional
    public void update(BuildingUpdateDTO buildingUpdateDTO) {

        // 将原业主关联的小区id设为0
        Building building1=buildingMapper.getById(buildingUpdateDTO.getId());
        Owner ownerOrigin=ownerMapper.getById(building1.getOwnerId());
        ownerOrigin.setCommunityId(0);
        ownerMapper.update(ownerOrigin);

        Owner ownerMapperByOwnerName = ownerMapper.getByOwnerName(buildingUpdateDTO.getOwnerName());
        Integer ownerId=null;
        if (ownerMapperByOwnerName==null) {
            //如果楼栋关联的业主在数据库没有记录的话，新增一条含有名字的业主记录
            Owner owner = new Owner();
            owner.setName(buildingUpdateDTO.getOwnerName());
            Community community=communityMapper.getByName(buildingUpdateDTO.getCommunityName());
            owner.setCommunityId(community.getId());
            ownerMapper.insert(owner);
            ownerId=owner.getId();
        }else{
            ownerId=ownerMapperByOwnerName.getId();
        }

        // 修改楼栋信息
        Building building = buildingMapper.getById(buildingUpdateDTO.getId());
        building.setStatusId(buildingUpdateDTO.getStatusId());
        building.setOwnerId(ownerId);
        building.setUpdateDate(LocalDate.now());
        buildingMapper.update(building);
    }

    /**
     * 根据小区id获取楼栋列表
     * @param communityId
     * @return
     */
    public List<BuildingSelectVO> list(Integer communityId) {
        List<BuildingSelectVO> list=buildingMapper.getByCommunityId(communityId);
        return list;
    }
}
